#include <QCoreApplication>
#include<iostream>
#include<QMutex>
#include<QThread>
#include<QObject>


//售票类
class ticketsllers:public QObject
{
public:
    ticketsllers();
    ~ticketsllers();

public slots:
    void salefunc();//售票

public:
    int *tickets;//票
    QMutex *metx;//互斥量
    std::string sellersname;//售票员姓名


};

ticketsllers::ticketsllers()
{
    metx=NULL;
    tickets=nullptr;
}
ticketsllers::~ticketsllers()
{

}

void ticketsllers::salefunc()
{
    while((*tickets)>0)
    {
        metx->lock();
        std::cout<<sellersname<<":"<<(*tickets)--<<std::endl;
        metx->unlock();
    }
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int ticket=20;
    QMutex mtx;

    //创建线程
    QThread th1;
    ticketsllers sells1;

    //设置线程
    sells1.tickets=&ticket;
    sells1.metx=&mtx;
    sells1.sellersname="seller kitty";

    //将对象移动到线程
    sells1.moveToThread(&th1);
    th1.start();
    QObject::connect(&th1,&QThread::started,&sells1,&ticketsllers::salefunc);

    return a.exec();
}
